package com.sy.core.web.hr;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.core.model.HrStaff;
import com.sy.core.service.HrStaffService;
import com.sy.core.util.RestResult;
import com.sy.core.web.BaseCtl;




/**
 * 	TODO 请用一句话描述该类的作用
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/hrStaff")
public class HrStaffCtl extends BaseCtl {

    @Autowired
    private HrStaffService  hrStaffService;
    

	@RequestMapping("/{path}")
	public ModelAndView toAnyPage(@PathVariable String path,ModelAndView mv){
	    mv.setViewName(path);
	    return mv;
    }
    
    /**
     * Action for Add
     */
    @PostMapping(value = "/save",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult save(HrStaff hrStaff,String jsonstr){
    	hrStaff = JSONObject.parseObject(jsonstr, HrStaff.class);
        RestResult rr = null;
        try {
        	if(hrStaff.getPk() == null){
        		hrStaff.setCreateTime(getCurrentTime());
        		hrStaff.setCreateBy(getLoginUser().getPk());
        		hrStaffService.insert(hrStaff);
        	}else {
        		hrStaff.setUpdateBy(getLoginUser().getPk());
        		hrStaff.setUpdateTime(getCurrentTime());
        		hrStaffService.update(hrStaff);
        	}
            rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            rr = new RestResult("201","FAILED");
        }
        return rr;
    }

    /**
     * Action for update
     */
    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult update(HrStaff hrStaff){
        RestResult rr = null;
        try {
        hrStaffService.update(hrStaff);
        rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
        e.printStackTrace();
        rr = new RestResult("201","FAILED");
        }
        return rr;
    }
    
    /**
     * Action for SelectOneByKey
     */
    @GetMapping(value = "/query/{key}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult queryByPrimaryKey(@PathVariable String key){
       try{
            HrStaff hrStaff = hrStaffService.queryByPrimaryKey(key);
      	 	return new RestResult("200","SUCCESS",hrStaff);
       }catch(Exception e){
    	   	logger.error("Query Model HrStaff By PrimaryKey Is Fail:"+e.getMessage());
       }
       		return new RestResult("201","FAIL");
    }
    
    /**
     * Action for SelectList
     */
    @RequestMapping(value = "/list",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult queryList(HrStaff hrStaff,String str){
       try{
    	   hrStaff = JSONObject.parseObject(str, HrStaff.class);
    	   JSONObject jsonObject = JSONObject.parseObject(str);
    	   PageHelper.startPage(Integer.parseInt(jsonObject.getString("currentPage")),
    			   Integer.parseInt(jsonObject.getString("pageSize")));
      	   List<HrStaff> hrStaffList = hrStaffService.queryByCondition(hrStaff);
      	   PageInfo<HrStaff> info = new PageInfo<HrStaff>(hrStaffList);
      	   return new RestResult("200","SUCCESS",info);
       }catch(Exception e){
    	   logger.error("Query Model HrStaff List Fail :"+e.getMessage());
    	   return new RestResult("201","FAIL");
       }
    }
    
    /**
     * Action for DeleteModel
     */
    @DeleteMapping("/delete/{pks}")
    @ResponseBody
    public RestResult delete(@PathVariable String pks){
        RestResult rr = null;
        try {
	        hrStaffService.batchDelete(pks);
	        rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
	        e.printStackTrace();
	        rr = new RestResult("201","FAILED");
        }
        return rr;
    }

   
}
